package com.kkb.retailer.center.server.service;


import com.kkb.retailer.center.api.MenuInfoService;
import com.kkb.retailer.center.common.pojo.MenuInfo;
import com.kkb.retailer.center.common.pojo.MenuMetaVo;
import com.kkb.retailer.center.common.pojo.MenuVo;
import com.kkb.retailer.center.common.pojo.RoleInfo;
import com.kkb.retailer.center.server.mappers.MenuInfoMapper;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuInfoServiceImpl implements MenuInfoService {

    @Autowired
    private MenuInfoMapper menuInfoMapper;


    /**
     * 根据角色列表获取菜单行列表
     * @param roleInfos
     * @return
     */
    @Override
    public List<MenuInfo> findByRoles(List<RoleInfo> roleInfos) {
        List<Long> roleIds = roleInfos.stream().map(i ->{
        Long role = i.getId();
        return role;
    }).collect(Collectors.toList());
        List<MenuInfo> list = menuInfoMapper.selectListByRoles(roleIds);
        return list;
    }

    /**
     * 构建菜单树状列表,包含子节点(重点！！！)
     * @param menuDtos
     * @return
     */
    @Override
    public HashMap<String, Object> buildTree(List<MenuInfo> menuDtos) {
        List<MenuInfo> trees = new ArrayList<>();
        Set<Long> ids = new HashSet<>();
        for(MenuInfo menuDto:menuDtos){
            if(menuDto.getPid()==0){
                trees.add(menuDto);
            }
            for(MenuInfo it: menuDtos){
                if(it.getPid().equals(menuDto.getId())){
                    if(menuDto.getChildren() == null){
                        menuDto.setChildren(new ArrayList<>());
                    }
                    menuDto.getChildren().add(it);
                    ids.add(it.getId());
                }
            }
        }
        HashMap<String,Object> map = new HashMap<>(2);
        if(trees.size()==0){
            trees = menuDtos.stream().filter(s ->
                    !ids.contains(s.getId())).collect(Collectors.toList());
        }
        map.put("content",trees);
        map.put("totalElements", menuDtos.size());
        return map;
    }

    @Override
    public List<MenuVo> buildMenus(List<MenuInfo> menuDtos) {
        List<MenuVo> list = new LinkedList<>();
        menuDtos.forEach(menuDTO -> {
                    if (menuDTO!=null){
                        List<MenuInfo> menuDtoList = menuDTO.getChildren();
                        MenuVo menuVo = new MenuVo();
                        menuVo.setName(StringUtils.isNotEmpty(menuDTO.getComponentName())  ? menuDTO.getComponentName() : menuDTO.getName());
                        // 一级目录需要加斜杠，不然会报警告
                        menuVo.setPath(menuDTO.getPid() == 0 ? "/" + menuDTO.getPath() :menuDTO.getPath());
                        menuVo.setHidden(menuDTO.getHidden());
                        // 如果不是外链

                        if(!menuDTO.getIFrame()){
                            if(menuDTO.getPid() == 0){
                                menuVo.setComponent(StringUtils.isEmpty(menuDTO.getComponent())?"Layout":menuDTO.getComponent());
                            }else if(!StringUtils.isEmpty(menuDTO.getComponent())){
                                menuVo.setComponent(menuDTO.getComponent());
                            }
                        }
                        menuVo.setMeta(new MenuMetaVo(menuDTO.getName(),menuDTO.getIcon(),!menuDTO.getCache()));
                        if(menuDtoList !=null && menuDtoList.size()!=0){
                            menuVo.setAlwaysShow(true);
                            menuVo.setRedirect("noredirect");
                            menuVo.setChildren(buildMenus(menuDtoList));
                            // 处理是一级菜单并且没有子菜单的情况
                        } else if(menuDTO.getPid() == 0){
                            MenuVo menuVo1 = new MenuVo();
                            menuVo1.setMeta(menuVo.getMeta());
                            // 非外链
                            if(!menuDTO.getIFrame()){
                                menuVo1.setPath("index");
                                menuVo1.setName(menuVo.getName());
                                menuVo1.setComponent(menuVo.getComponent());
                            } else {
                                menuVo1.setPath(menuDTO.getPath());
                            }
                            menuVo.setName(null);
                            menuVo.setMeta(null);
                            menuVo.setComponent("Layout");
                            List<MenuVo> list1 = new ArrayList<>();
                            list1.add(menuVo1);
                            menuVo.setChildren(list1);
                        }
                        list.add(menuVo);
                    }
                }
        );
        return list;
    }
}
